from django import template
register = template.Library()

from django.utils.html import format_html
from myadmin import models

from django.core.urlresolvers import reverse

# 自定义标签,乘法
@register.simple_tag

def ShowTotal(num,price):
    res = num*price

    return res

# 自定义标签 分页
@register.simple_tag

def showpage(count,request):
    # 获取页面的搜索条件 &types=phone&keywords=11
    data = request.GET.dict()

    u = ''
    for k,v in data.items():
        if k != 'page':
            u += '&{k}={v}'.format(k=k,v=v)

    # 获取当前页码
    p = int(request.GET.get('page',1))
    # 开始页
    begin = p - 4
    # 结束页
    end = p + 5
    # 判断如果当前页小于5
    if p < 5:
        # 则开始页为1
        begin = 1
        end = 10
    # 如果总页码小于10
    if count < 10:
        begin = 1
        end = count
    # 当前页面大于总页数减去5
    if p > count-5:
        # 则开始页为总页数-9
        begin = count - 9
        # 结束页为总页数
        end = count

    # 判断如果开始页 小于或者等于  0 ,则开始页为1 
    if begin <= 0 :
        begin = 1

    s = ''
    s += '<li><a href="?page=1'+u+'">首页</li>'
    # 上一页
    if p == 1:
        s += '<li class="am-disabled"><a href="?page=1'+u+'">«</li>'
    else:
        s += '<li><a href="?page='+str(p-1)+u+'">«</a></li>'



    for x in range(begin,end+1):
        if p == x:
            s += '<li class="am-active"><a href="?page='+str(x)+u+'">'+str(x)+'</a></li>'    
        else:
            s += '<li><a href="?page='+str(x)+u+'">'+str(x)+'</a></li>'


    # 下一页
    if p == count:
        s += '<li class="am-disabled"><a href="?page='+str(count)+u+'">»</li>'
    else:
        s += '<li><a href="?page='+str(p+1)+u+'">»</a></li>'


    s += '<li><a href="?page='+str(count)+u+'">尾页</a></li>'

    return format_html(s)
# ---------------------------------------
    # s = ''
    # s += '<li><a href="?page=1'+u+'">首页</a></li>'
    # # 上一页
    # if p == 1:
    #     s += '<li class="am-disabled"><a href="?page=1'+u+'">«</a></li>'
    # else:
    #     s += '<li><a href="?page='+str(p-1)+u+'">«</a></li>'
    
    # for x in range(begin,end+1):
    #     if p == x:
    #         s += '<li class="am-active"><a href="?page='+str(x)+u+'">'+str(x)+'</a></li>'
    #     else:
    #         s += '<li><a href="?page='+str(x)+u+'">'+str(x)+'</a></li>'

    # # 下一页
    # if p == count:
    #     s += '<li class="am-disabled"><a href="?page='+str(count)+u+'">»</a></li>'
    # else:
    #     s += '<li><a href="?page='+str(p+1)+u+'">»</a></li>'
    # s += '<li><a href="?page='+str(count)+u+'">尾页</a></li>'

    # return format_html(s)


# @register.simple_tag

# def row():

#     data = models.Types.objects.exclude(pid=0)
#     a = ""
#     b = []
#     for x in data:
#         # 二级对象
#         b.append(x.name)
#     for i in b:

#         a += '<li class="layout-header-nav-item"><a href="./list.html" class="layout-header-nav-link">%s</a></li>'%(i)
#         # print(x.items)
#     # context['tlist'] = data

#     return format_html(a)
# #    

# # 自定义标签 头部导航
# from django.db.models import Q

# @register.simple_tag
# def row():

#     data = models.Types.objects.filter(pid=0)
#     a = ""
    
#     for x in data:
#         # 二级对象
#         er = models.Types.objects.filter(~Q(pid=0))

#     for y in er:
        
#         a += '<li class="layout-header-nav-item"><a href="#" class="layout-header-nav-link">%s</a></li>'%(y.name)
#         # print(x.items)
#     # context['tlist'] = data
    
#     return format_html(a)


 # 自定义标签 头部导航
@register.simple_tag
def row():
    
    # 获取所有的二级分类
    erlist = models.Types.objects.exclude(pid=0)

    nav = ''
    for x in erlist:
        nav += '''
        <li class="layout-header-nav-item">
            <a href="{url}" class="layout-header-nav-link">{name}</a>
        </li>
        '''.format(name=x.name,url=reverse('myhome_list',args=(x.id,)))
    return format_html(nav)
